/**
 * 设置我的头像
 */
function setMyHeader() {
    let me = getMe();
    if (me != null && !isNull(me.userName)){
        $('.no-login').hide();
        $('.myself').show();
    }else {
        setNoLogin();
    }
}

/**
 * 退出登录
 */
function logoutVent() {
    $.ajax({
        type: 'post',
        url : '/ventUserIndex/toLogout',
        dataType: 'json',
        success: function() {
            logoutSuccess();
        },
        error: function() {
            logoutSuccess();
        }
    });
}

/**
 * 退出成功
 */
function logoutSuccess() {
    localStorage.removeItem('me');
    setNoLogin();
}

/**
 * 设置未登录状态
 */
function setNoLogin() {
    $('.no-login').show();
    $('.myself').hide();
}
$(function() {
    let asKey = 'accessSiteTotal_', now = getDay(0);
    let accessSiteTotal = localStorage.getItem(asKey + now);
    let me = localStorage.getItem('me');
    if (me == null || me == ''){
        return;
    }
    me = JSON.parse(me);
    let myId = me.userId;
    //如果满足以下条件，则不再请求：1、accessSiteTotal不为空，说明已统计过；2、如果用户ID为空
    if (accessSiteTotal != null || myId == null || myId == ''){
        return;
    }
    delAccessSiteTotalKey();
    localStorage.setItem(asKey + now,'1');
    $.ajax({
        type: 'post',
        url : '/ventIndex/accessSiteAddOne',
        dataType: 'json',
        async: true,
        data: {userId: myId},
        timeout: 12000,
        success: function () {
        }
    });
});

/**
 * 获取当前日期的前几天
 * @param day -数字：表示获取前几天
 * @returns {string} 日期
 */
function getDay(day){
    let today = new Date();
    let targetDayMilliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
    today.setTime(targetDayMilliseconds); //注意，这行是关键代码
    let tYear = today.getFullYear(), tMonth = today.getMonth(), tDate = today.getDate();
    tMonth = doHandleMonth(tMonth + 1);
    tDate = doHandleMonth(tDate);
    return tYear + tMonth + tDate;
}
function doHandleMonth(month){
    return month < 10 ? '0' + month : month;
}

/**
 * 删除accessSiteTotal中其它的key
 */
function delAccessSiteTotalKey() {
    for (let i = 0; i < localStorage.length; i ++) {
        let asKey = localStorage.key(i); //获取本地存储的Key
        //如果Key以accessSiteTotal_开头
        if (asKey.indexOf('accessSiteTotal_') == 0){
            localStorage.removeItem(asKey);
        }
    }
}